﻿@{
    
    Layout = null;
}

    <link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

    <form class="layui-form" action="" style="margin-top:30px">
        <div class="layui-form-item">
            <label class="layui-form-label">单位编码</label>
            <div class="layui-input-block">
                <input type="text" name="unitMeasureCode" lay-verify="required" placeholder="请输入单位编码" autocomplete="off" class="layui-input" style="width:300px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单位名称</label>
            <div class="layui-input-block">
                <input type="text" name="unitMeasureName" lay-verify="required" placeholder="请输入单位名称" autocomplete="off" class="layui-input" style="width:300px">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否是主单位</label>
            <div class="layui-input-block">
                <input type="radio" id="isPrimaryUnitYes" name="isPrimaryUnit" lay-filter="isPrimaryUnits" value="true" title="是" checked>
                <input type="radio" id="isPrimaryUnitNo" name="isPrimaryUnit" lay-filter="isPrimaryUnits" value="false" title="否">
            </div>
        </div>
        <div id="mainUnitFieIds" style="display:none;">
            <div class="layui-form-item">
                <label class="layui-form-label">主单位</label>
                <div class="layui-input-block">
                    <select id="vendorSelect" name="mainUnit" lay-search style="width:300px">
                        <option value="">请选择主单位</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">与主单位换算比例</label>
                <div class="layui-input-block">
                    <input type="text" id="conversionRatio" name="conversionScale" placeholder="请输入换算比例" autocomplete="off" class="layui-input" style="width:300px">
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input type="radio" id="stateYes" name="state" value="true" title="是" checked>
                <input type="radio" id="stateNo" name="state" value="false" title="否">

            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="add">确定</button>
                <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkStation()">取消</button>
            </div>
        </div>
    </form>

  
    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
    <script>
        //关闭弹框
        function closeWorkStation() {
            var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
            parent.layer.close(index);//再执行关闭
        }
        layui.use(['form', 'laydate', 'util'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var util = layui.util;
            $('#mainUnit').val('');
            $('#conversionRatio').val('');

            // 监听是否主单位的选择
            form.on('radio(isPrimaryUnits)', function (data) {
                var value = data.value;// 获取选中的值
                if (value == "true") {
                    // 如果是主单位，隐藏额外的字段
                    $('#mainUnitFieIds').hide();

                }
                else {
                    // 如果不是主单位，显示额外的字段
                    $('#mainUnitFieIds').show();
                }
            })

            // 提交事件
            form.on('submit(add)', function (data) {
                var field = data.field; // 获取表单字段值
                $.ajax({
                    type: 'post',
                    url: '/UnitMeasure/CreateUnitMeasure',
                    data: field,
                    dataType:'json',
                    success: function (res) {
                        if (res > 0) {
                            layer.msg('添加成功')
                            closeWorkStation()
                            return false;
                        }
                        if (res == -1) {
                            layer.msg('单位编号已存在')
                            return false;
                        }
                        if (res == -2) {
                            layer.msg('单位名称已存在')
                            return false;
                        }
                    },

                })
                return false; // 阻止默认 form 跳转
            });


            $(document).ready(function () {
                // 使用 AJAX 获取主单位数据  
                $.ajax({
                    url: '/UnitMeasure/GetUnitMeasure', // 替换为你的 API 路径  
                    type: 'GET',
                    dataType: 'json',
                    async: false,
                    success: function (res) {
                        // 清空现有选项  
                        $('#vendorSelect').empty();
                        $('#vendorSelect').append('<option value="0">请选择主单位</option>');

                        // 遍历返回的数据  
                        $.each(res, function (index, vendor) {
                            $('#vendorSelect').append(
                                '<option value="' + vendor.id + '">' + vendor.unitMeasureName + '</option>'
                            );
                        });
                        // 重新渲染 layui 组件
                            layui.form.render('select');
                    },
                    error: function (xhr, status, error) {
                        console.error('获取供应商失败:', error);
                    }
                });

                // 监听下拉框变化  
                $('#vendorSelect').change(function () {
                    var selectedValue = $(this).val();
                    console.log("选择的供应商ID:", selectedValue);
                    // 根据选择的主单位进行其他操作  
                });
            });  

            });

    </script>